package com.plugin.mybatis.generator;


import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.ObjectFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xiangshiming
 */
public class ServiceCodePluginMinI extends PluginAdapter {


//	FullyQualifiedJavaType javaUtil = new FullyQualifiedJavaType("java.util.*");
//	FullyQualifiedJavaType manager = new FullyQualifiedJavaType("cn.hlb.platform.common.mybatis.manager.Manager");



	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles() {

		return super.contextGenerateAdditionalJavaFiles();
	}




	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(
			IntrospectedTable introspectedTable) {
		List<GeneratedJavaFile> javafiles = new ArrayList<GeneratedJavaFile>();
		properties.get("targetProject");
		properties.get("targetPackage");
		String targetProject = this.getProperties().getProperty("targetProject");
		String targetPackage =this.getProperties().getProperty("targetPackage");
		String managerRootInterface =this.getProperties().getProperty("managerRootInterface");
		String modelTargetPackage = context.getJavaModelGeneratorConfiguration().getTargetPackage();

		JavaFormatter javaFormatter = ObjectFactory.createJavaFormatter(context) ;

		//service 接口
		FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
		String interPage = targetPackage + "." + table.getDomainObjectName()
				+ "Service";
		FullyQualifiedJavaType serviceType = new FullyQualifiedJavaType(interPage);
		try {
			if(Class.forName(interPage)!=null){
				return javafiles;
			}
		} catch (ClassNotFoundException e) {

		}
		Interface interfaze = new Interface(serviceType);
		interfaze.setVisibility(JavaVisibility.PUBLIC);
		interfaze.addImportedType(new FullyQualifiedJavaType(managerRootInterface));
//		interfaze.addImportedType(javaUtil);
		interfaze.addImportedType(new FullyQualifiedJavaType(modelTargetPackage+"."+table.getDomainObjectName()));
		interfaze.addImportedType(new FullyQualifiedJavaType(modelTargetPackage+"."+table.getDomainObjectName()+"Example"));

		FullyQualifiedJavaType managerInterfaceType = new FullyQualifiedJavaType(managerRootInterface+"<"+table.getDomainObjectName()+","+table.getDomainObjectName()+"Example"+">");

		interfaze.addSuperInterface(managerInterfaceType);

		GeneratedJavaFile generatedJavafaceFile = new GeneratedJavaFile(interfaze, targetProject, javaFormatter);

		//sercice 实现类
		interPage = targetPackage+".impl."+table.getDomainObjectName()+"ServiceImpl";

		FullyQualifiedJavaType typeImp = new FullyQualifiedJavaType(interPage);
		TopLevelClass topLevelClass = new TopLevelClass(typeImp);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);

		FullyQualifiedJavaType managerImplInterfaceType = new FullyQualifiedJavaType(managerRootInterface+"Impl"+"<"+table.getDomainObjectName()+","+table.getDomainObjectName()+"Example"+">");
		topLevelClass.addImportedType(managerRootInterface+"Impl");
		topLevelClass.setSuperClass(managerImplInterfaceType);

		topLevelClass.addSuperInterface(serviceType);
		topLevelClass.addImportedType(serviceType);
		topLevelClass.addImportedType(new FullyQualifiedJavaType(modelTargetPackage+"."+table.getDomainObjectName()+"Example"));

		Field field = new Field();
		field.setVisibility(JavaVisibility.PRIVATE);
		FullyQualifiedJavaType daoInfaceType = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
		field.setType(daoInfaceType);
		String shortName = "thisMapper";
		field.setName(shortName);
		topLevelClass.addField(field );
		topLevelClass.addImportedType("org.springframework.beans.factory.annotation.Autowired");
		topLevelClass.addImportedType("org.springframework.stereotype.Service");
		topLevelClass.addImportedType(FullyQualifiedJavaType.getNewMapInstance());


		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * xiangsm@songcw.com ");
		topLevelClass.addJavaDocLine(" * Tool generation time " + new Date().toString());
		topLevelClass.addJavaDocLine(" */");

		topLevelClass.addAnnotation("@Service");
		topLevelClass.addImportedType(daoInfaceType);

		FullyQualifiedJavaType parameterType =  new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
		topLevelClass.addImportedType(parameterType);


		Method method = new Method();
		method = new Method();
		method.addAnnotation("@Autowired");
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName(table.getDomainObjectName()+"ServiceImpl");
		method.addParameter(new Parameter(daoInfaceType, shortName));
		method.addBodyLine("this.mapper = "+shortName+";");
		method.addBodyLine("this.thisMapper = "+shortName+";");
		topLevelClass.addMethod(method );

		GeneratedJavaFile generatedJavaFile= new GeneratedJavaFile(topLevelClass, targetProject, javaFormatter);


		javafiles.add(generatedJavaFile);
		javafiles.add(generatedJavafaceFile);

		return javafiles;
	}



}
